<!DOCTYPE HTML PUBLIC "-//ORA//DTD CD HTML 3.2//EN">
<HTML>
<HEAD>
<TITLE>[Chapter 5] 5.7 Variable and Method Visibility</TITLE>
<META NAME="author" CONTENT="Pat Niemeyer and Josh Peck">
<META NAME="date" CONTENT="Tue Jul 22 18:52:56 1997">
<META NAME="form" CONTENT="html">
<META NAME="metadata" CONTENT="dublincore.0.1">
<META NAME="objecttype" CONTENT="book part">
<META NAME="otheragent" CONTENT="gmat dbtohtml">
<META NAME="publisher" CONTENT="O'Reilly &amp; Associates, Inc.">
<META NAME="source" CONTENT="SGML">
<META NAME="subject" CONTENT="Java">
<META NAME="title" CONTENT="Exploring Java">
<META HTTP-EQUIV="Content-Script-Type" CONTENT="text/javascript">
</HEAD>
<body vlink="#551a8b" alink="#ff0000" text="#000000" bgcolor="#FFFFFF" link="#0000ee">

<DIV CLASS=htmlnav>
<H1><a href='index.htm'><IMG SRC="gifs/smbanner.gif"
     ALT="Exploring Java" border=0></a></H1>
<table width=515 border=0 cellpadding=0 cellspacing=0>
<tr>
<td width=172 align=left valign=top><A HREF="ch05_06.htm"><IMG SRC="gifs/txtpreva.gif" ALT="Previous" border=0></A></td>
<td width=171 align=center valign=top><B><FONT FACE="ARIEL,HELVETICA,HELV,SANSERIF" SIZE="-1">Chapter 5<br>Objects in Java</FONT></B></TD>
<td width=172 align=right valign=top><A HREF="ch05_08.htm"><IMG SRC="gifs/txtnexta.gif" ALT="Next" border=0></A></td>
</tr>
</table>

&nbsp;
<hr align=left width=515>
</DIV>
<DIV CLASS=sect1>
<h2 CLASS=sect1><A CLASS="TITLE" NAME="EXJ-CH-5-SECT-7">5.7 Variable and Method Visibility</A></h2>

<P CLASS=para>
<A NAME="CH05.ENCAP1"></A>One of the most important aspects of object-oriented design is data hiding, 
or <I CLASS=emphasis>encapsulation</I>.
By treating an object in some respects as a "black box" and 
ignoring the details of its implementation, we can write stronger, simpler 
code with components that can be easily reused. 

<DIV CLASS=sect2>
<h3 CLASS=sect2><A CLASS="TITLE" NAME="EXJ-CH-5-SECT-7.1">Basic Access Modifiers</A></h3>

<P CLASS=para>
<A NAME="CH05.VIS1"></A><A NAME="CH05.VIS2"></A><A NAME="CH05.VIS3"></A><A NAME="CH05.VIS4"></A>By default, the variables and methods of a class are accessible to
members of the class itself and other classes in the same package. To
borrow from C++ terminology, classes in the same package are
<tt CLASS=literal>friendly</tt>. We'll call this the default level
of visibility. As you'll see as we go on, the default visibility
lies in the middle of the range of restrictiveness that can be
specified.

<P CLASS=para>
The modifiers <tt CLASS=literal>public</tt> and
<tt CLASS=literal>private</tt>, on the other hand, define the extremes. As
we mentioned earlier, methods and variables declared as
<tt CLASS=literal>private</tt> are accessible only within their class. At
the other end of the spectrum, members declared as
<tt CLASS=literal>public</tt> are always accessible, from any class in any
package. Of course, the class that contains the methods must also be
<tt CLASS=literal>public</tt>, as we just discussed. The
<tt CLASS=literal>public</tt> members of a class should define its most
general functionality--what the black box is supposed to do.
<A HREF="ch05_07.htm#EXJ-CH-5-FIG-8">Figure 5.8</A> illustrates the three simplest levels
of visibility.

<DIV CLASS=figure>
<h4 CLASS=figure><A CLASS="TITLE" NAME="EXJ-CH-5-FIG-8">Figure 5.8: Private, default, protected, and public visibility</A></h4>


<p>
<img align=middle src="./figs/je0508.gif" alt="[Graphic: Figure 5-8]" width=503 height=203 border=0>

</DIV>

<P CLASS=para>
<A HREF="ch05_07.htm#EXJ-CH-5-FIG-8">Figure 5.8</A> continues with the example from
the previous section. Public members in <tt CLASS=literal>TextArea</tt>
are accessible from anywhere. Private members are not visible from
outside the class. The default visibility allows access by other
classes in the package.

<P CLASS=para>
The <tt CLASS=literal>protected</tt> modifier allows special access
permissions for subclasses. Contrary to how it might sound,
<tt CLASS=literal>protected</tt> is slightly less restrictive than the
default level of accessibility. In addition to the default access
afforded classes in the same package, <tt CLASS=literal>protected</tt>
members are visible to subclasses of the class, even if they are
defined in a different package. If you are a C++ programmer and so
used to more restrictive meanings for both the default and
<tt CLASS=literal>protected</tt> levels of access, this may rub you the
wrong way.

<DIV CLASS=sect3>
<h4 CLASS=sect3><A CLASS="TITLE" NAME="ch05-SECT3-AUTOID.4">What was private protected?</A></h4>

<P CLASS=para>
Early on, the Java language allowed for certain combinations of modifiers,
one of which was <tt CLASS=literal>private protected</tt>.
The meaning of private protected was to limit visibility strictly to 
subclasses (and remove package access).  This was later deemed somewhat
inconsistent and overly complex and is no longer supported.[5]

<blockquote class=footnote>
<P CLASS=para>[5] 
The meaning of the <tt CLASS=literal>protected</tt> modifier changed in
the Beta2 release of Java, and the <tt CLASS=literal>private protected</tt>
combination appeared at the same time. They patched some potential
security holes, but confused many people.
</blockquote>
<DIV CLASS=programlisting>
<P>
<PRE>
</PRE>
</DIV>

<P CLASS=para>
<A HREF="ch05_07.htm#EXJ-CH-5-TAB-1">Table 5.1</A> summarizes the levels of
visibility available in Java; it runs generally from most restrictive
to least. Methods and variables are always visible within a class, so
the table doesn't address those:

<P>
<DIV CLASS=table>
<TABLE BORDER>
<CAPTION><A CLASS="TITLE" NAME="EXJ-CH-5-TAB-1">Table 5.1: Visibility Modifiers</A></CAPTION>
<TR CLASS=row>
<TH ALIGN="left">Modifier</TH>
<TH ALIGN="left">Visibility</TH>
</TR>
<TR CLASS=row>
<TD ALIGN="left"><tt CLASS=literal>private</tt></TD>
<TD ALIGN="left">None</TD>
</TR>
<TR CLASS=row>
<TD ALIGN="left">none (default)</TD>
<TD ALIGN="left">Classes in the package</TD>
</TR>
<TR CLASS=row>
<TD ALIGN="left"><tt CLASS=literal>protected</tt></TD>
<TD ALIGN="left">Classes in package and subclasses inside or outside the package</TD>
</TR>
<TR CLASS=row>
<TD ALIGN="left"><tt CLASS=literal>public</tt></TD>
<TD ALIGN="left">All classes</TD>
</TR>
</TABLE>
<P>
</DIV>
</DIV>

</DIV>

<DIV CLASS=sect2>
<h3 CLASS=sect2><A CLASS="TITLE" NAME="EXJ-CH-5-SECT-7.2">Subclasses and Visibility</A></h3>

<P CLASS=para>
There are two important (but unrelated) notes we need to add to the
discussion of visibility with regards to class members in
subclasses. First, when you override methods of a class in a subclass,
it's not possible to reduce their visibility. While it is possible to
take a <tt CLASS=literal>private</tt> method of a class and override it to
be <tt CLASS=literal>public</tt> in a subclass, the reverse is not
possible. This makes sense when you think about the fact that subtypes
have to be usable as instances of their supertype (e.g., a
<tt CLASS=literal>Mammal</tt> is a type of <tt CLASS=literal>Animal</tt>).  If
we could reduce the visibility of an overridden method, this would be
a problem. However, we can reduce the visibility of a variable because
it simply results in a shadowed variable. As with all shadowed
variables, the two variables are distinct and can have separate
visibilities in their different class forms.

<P CLASS=para>
The second point is that <tt CLASS=literal>protected</tt> variables
of a class are visible to its subclasses, but unlike C++, only in
objects of the subclass's type or its subtypes. In other words,
a subclass can see a <tt CLASS=literal>protected</tt> variable from its
superclass as an inherited variable, but it can't access the variable in
a separate instance of the superclass itself.
This can be confusing because often we forget that visibility modifiers
don't resrtict between multiple instances of the same class in the same way 
that they do instances of different classes.  Two instances of the same type 
of object can normally access all of each other's members, 
including private ones.  Said another way: two instances of Cat can access
all of each other's variables and methods (including private ones), but a 
Cat can't access a protected member in an instance of Animal, unless it 
can prove that the Animal is a Cat.

<P CLASS=para>
</DIV>

</DIV>


<DIV CLASS=htmlnav>

<P>
<HR align=left width=515>
<table width=515 border=0 cellpadding=0 cellspacing=0>
<tr>
<td width=172 align=left valign=top><A HREF="ch05_06.htm"><IMG SRC="gifs/txtpreva.gif" ALT="Previous" border=0></A></td>
<td width=171 align=center valign=top><a href="index.htm"><img src='gifs/txthome.gif' border=0 alt='Home'></a></td>
<td width=172 align=right valign=top><A HREF="ch05_08.htm"><IMG SRC="gifs/txtnexta.gif" ALT="Next" border=0></A></td>
</tr>
<tr>
<td width=172 align=left valign=top>Packages and Compilation Units</td>
<td width=171 align=center valign=top><a href="index/idx_0.htm"><img src='gifs/index.gif' alt='Book Index' border=0></a></td>
<td width=172 align=right valign=top>Interfaces</td>
</tr>
</table>
<hr align=left width=515>

<IMG SRC="gifs/smnavbar.gif" USEMAP="#map" BORDER=0> 
<MAP NAME="map"> 
<AREA SHAPE=RECT COORDS="0,0,108,15" HREF="../javanut/index.htm"
alt="Java in a Nutshell"> 
<AREA SHAPE=RECT COORDS="109,0,200,15" HREF="../langref/index.htm" 
alt="Java Language Reference"> 
<AREA SHAPE=RECT COORDS="203,0,290,15" HREF="../awt/index.htm" 
alt="Java AWT"> 
<AREA SHAPE=RECT COORDS="291,0,419,15" HREF="../fclass/index.htm" 
alt="Java Fundamental Classes"> 
<AREA SHAPE=RECT COORDS="421,0,514,15" HREF="../exp/index.htm" 
alt="Exploring Java"> 
</MAP>
</DIV>

</BODY>
</HTML>
